package com.jasonm.rssminer.dao;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.jasonm.rssminer.dto.FeedDTO;

public class SolrDAO {

	@Autowired
	@Qualifier("solrServer")
	private SolrServer solrServer;
	static private Logger log = LoggerFactory.getLogger(SolrDAO.class);

	public void addFeedToSolr(List<FeedDTO> feeds) {

		Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();

		for (FeedDTO feed : feeds) {
			SolrInputDocument doc = new SolrInputDocument();
			doc.addField("id", feed.getId());
			doc.addField("link", feed.getLink());
			doc.addField("title", feed.getTitle());
			doc.addField("description", feed.getDescription());
			doc.addField("date", feed.getPubDate());
			docs.add(doc);
		}

		try {
			solrServer.add(docs);
			solrServer.commit();
		} catch (SolrServerException e) {
			log.error("Problem adding to Solr:" + e.getMessage());
		} catch (IOException e) {
			log.error("Problem with IO and Solr:" + e.getMessage());
		}

	}

	public void deleteAll() {
		try {
			solrServer.deleteByQuery("*:*");
			solrServer.commit();
		} catch (SolrServerException e) {
			log.error("Problem deleting all elements in Solr:" + e.getMessage());
		} catch (IOException e) {
			log.error("Problem with IO and Solr:" + e.getMessage());
		}

	}

	public List<FeedDTO> queryFeeds(String search) {

		List<FeedDTO> feeds = new ArrayList<FeedDTO>();

		SolrQuery query = new SolrQuery();
		query.setQuery(search);
		SolrDocumentList list = null;

		try {
			QueryResponse rsp = solrServer.query(query);
			list = rsp.getResults();

		} catch (SolrServerException e) {
			log.error("Error getting SOLR response=" + e.getMessage());
		}

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSz");
		for (SolrDocument doc : list) {
			FeedDTO feed = null;
			//try {// 2011-03-31T05:55:01.061Z
					// yyyy-MM-ddTHH:mm:ss.SSSz
				feed = new FeedDTO((String) doc.getFieldValue("id"),
						(String) doc.getFieldValue("title"),
						(String) doc.getFieldValue("description"),
						(String) doc.getFieldValue("link"),null);
						//(sdf.parse((String) doc.getFieldValue("date"))));
//			} catch (ParseException e) {
//				log.error("Parsing Date Error=" + e.getMessage());
//			}
			feeds.add(feed);
		}

		return feeds;

	}
}
